<template>
  <div class="thumbnail-box">
     <el-upload
      :auto-upload="true"
      :on-change="handleUploadChange"
      :before-upload="handleUploadBefore"
      :show-file-list="false"
      name ="file"
      :disabled="coverImageUrl ? true : false"
      :action="baseurl+'/update/file'"
      :headers="headers"
      :data="{pathSuffix: path}"
      accept="image/png, image/jpeg"
      class="thumbnail-uploader"
      :on-success="success"
    >
      <!-- preview -->
      <div v-if="coverImageUrl" class="thumbnail">
        <img :src="coverImageUrl" class="thumbnail-img" />
        <span class="thumbnail-actions">
          <span class="thumbnail-actions__preview" @click="handleCoverImagePreview">
            <i class="el-icon-zoom-in" />
          </span>
          <span v-if="!disabled" class="thumbnail-actions__delete" @click="handleCoverImageRemove">
            <i class="el-icon-delete" />
          </span>
        </span>
      </div>
      <!-- upload icon -->
      <i v-else class="el-icon-plus thumbnail-uploader-icon" />
      <div slot="tip" class="el-upload__tip">
        <span :class="{ 'cover-file__error': coverImageError.type }">只能上传PNG/JPG文件</span>,
        像素大于28*28 且
        <span
          :class="{ 'cover-file__error': coverImageError.size }"
        >不超过500KB.</span>
      </div>
    </el-upload>
    <!-- 缩略图放大预览 -->
    <el-dialog :visible.sync="coverImagePreviewDialog" :append-to-body="true" class="cover-preview__dialog">
      <img :src="coverImageUrl" width="100%" alt />
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
export default {
  props: {
    url: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    // path取值： advise 通知  cartoon 漫画 chapter 章节 carousel 轮播
    path: {
      type: String,
      default: ''
    },
  },
   watch:{
    url:function(newVal,oldVal){
      console.log(newVal)
      this.coverImageUrl = newVal;
    }
  },
  data () {
    return {
      baseurl:process.env.API_BASEURL,
      coverImagePreviewDialog: false,
      coverImageUrl: "",
      headers:{},
      coverImageError: {
        size: false,
        type: false
      }
    }
  },
  mounted(){

    if(this.url!=""){
      this.coverImageUrl = this.url;
    }

    this.headers['Authorization'] = getToken();
  },
  methods: {
    checkFileSize(file) {
      const size = file.size / 1024;
      if (size <= 500) {
        this.coverImageError.size = false;
        return true;
      } else {
        this.coverImageError.size = true;
        this.$message({
          type: 'warning',
          message: '请选择不超过500KB的图片'
        })
        return false;
      }
    },
    checkFileType(file) {
      const fileExt = file.name.replace(/.+\./, "");
      if (/(png|jpe?g|PNG|JPE?G)/.test(fileExt)) {
        this.coverImageError.type = false;
        return true;
      } else {
        this.coverImageError.type = true;
        return false;
      }
    },
    /**
     * 上传文件改变触发
     */
    handleUploadChange(file) {
      console.log(file);
      if (this.checkFileType(file) && this.checkFileSize(file)) {
        this.coverImageUrl = URL.createObjectURL(file.raw);
        //this.changeFile(file)
      }
    },
    /**
     * 上传文件前判断大小是否符合要求
     */
    handleUploadBefore (file) {
      return this.checkFileSize(file)
    },
    /**
     * 缩略图预览
     */
    handleCoverImagePreview() {
      this.coverImagePreviewDialog = true;
    },
    /**
     * 删除缩略图
     */
    handleCoverImageRemove() {
      this.coverImageUrl = "";
      this.changeFile()
    },
    success(response, file, fileList){
     
      if(response.code == 1){
        this.changeFile(response.data)
      }else{
        alert(response.message)
      }
    },
    changeFile (file) {
      this.$emit('changelogofile', file)
    }
  }
}
</script>

<style lang="scss">
.thumbnail-box{
  .thumbnail-uploader .el-upload {
    border: 1px dashed #36c6d3;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .thumbnail-uploader .el-upload:hover {
    border-color: #36c6d3;
  }
  .thumbnail-uploader-icon {
    font-size: 20px;
    color: #8c939d;
    width: 80px;
    height: 80px;
    line-height: 80px;
    text-align: center;
  }
  .thumbnail {
    width: 80px;
    height: 80px;
    display: flex;
    align-items: center;
    position: relative;
    .thumbnail-img {
      max-width: 100%;
    }
    .thumbnail-actions {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      cursor: default;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, 0.5);
      transition: opacity 0.3s;
      &::after {
        display: inline-block;
        content: "";
        vertical-align: middle;
        height: 100%;
      }
      &:hover {
        opacity: 1;
        span {
          display: inline-block;
        }
        span + span {
          margin-left: 10px;
        }
      }
      & span {
        font-size: 16px;
        display: none;
        cursor: pointer;
      }
    }
  }
}
.cover-preview__dialog {
  .el-dialog__header {
    background: #fff;
    border: none;
  }
}
</style>